package yja.gzw.app.activity

import android.view.LayoutInflater
import android.view.View
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.google.android.material.bottomsheet.BottomSheetDialog
import yja.gzw.app.R
import yja.gzw.app.adapter.ChujiaAdapter
import yja.gzw.app.adapter.NeituAdapter
import yja.gzw.app.adapter.ZhuanquAdapter
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityJingspxiangqingBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel

class JingspxiangqingActivity :BaseActivity<Mainviewmodel, ActivityJingspxiangqingBinding>(R.layout.activity_jingspxiangqing){
    private lateinit var bottomSheetDialog: BottomSheetDialog

    private lateinit var neituada: ChujiaAdapter
    override fun initView() {

        binding.fanhui.setOnClickListener {

            finish()
        }

        val id=intent.getStringExtra("id")

        viewmodel.onxiangqingwang(UserData.token, mapOf("order_id" to id!!))

        binding.qiangdan.setOnClickListener {
            bottomSheetDialog = BottomSheetDialog(this)
            val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_chujia, null)
            bottomSheetDialog.setContentView(view)
            val chacah=view.findViewById<ImageView>(R.id.chacha)
            val queding=view.findViewById<TextView>(R.id.queding)
            val chujiarushukuang=view.findViewById<EditText>(R.id.chujiarushukuang)
            chacah.setOnClickListener {
                bottomSheetDialog.dismiss()
            }

            queding.setOnClickListener {


                val zhi = chujiarushukuang.text.toString().toIntOrNull()

                if(zhi!!<=40){
                    Toast.makeText(this, "价格不能低于40", Toast.LENGTH_SHORT).show()
                }else{
                    viewmodel.onchujiahejingjiawang(UserData.token,mapOf("order_id" to id,"price" to zhi.toString()))

                }



            }


            bottomSheetDialog.show()
        }


    }

    override fun dataObservese() {

        viewmodel.onchujiahejingjia.observe(this, Observer {
            if(it.code==1){
                bottomSheetDialog.dismiss()
                    finish()
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })

        viewmodel.onqiangdan.observe(this, Observer {
            if(it.code==1){
                Toast.makeText(this, "抢单成功", Toast.LENGTH_SHORT).show()
                finish()
            }else{
                Toast.makeText(this, "抢单失败", Toast.LENGTH_SHORT).show()

            }


        })

        viewmodel.onwodezhuye.observe(this, Observer {
            if(it.code==1){


                binding.fensiguanzhu.text=""+it.data.fav_count+"获赞     "+it.data.focus_count+"关注"

            }


        })


      viewmodel.onxiangqing.observe(this, Observer {
          if(it.code==1){

                binding.jingjiarengshu.text="竞价人数：${it.data.price_person_num}人"

              val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
              Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.publisher.avatar)
                  .apply(requestOptions)
                  .into(binding.touxiang)


              binding.jingjiarengsherengshow.layoutManager=
                  LinearLayoutManager(this)
              neituada= ChujiaAdapter(this,it.data.price_list)
              binding.jingjiarengsherengshow.adapter= neituada




              if(it.data.material!=null||it.data.video_url==null){
                  binding.shiping.visibility=View.GONE
                    val imageUrls = it.data.material.split(",")

                  binding.xbaner.setData(imageUrls,null)
                  binding.xbaner.setmAutoPalyTime(3000)
                  binding.xbaner.setmAutoPlayAble(true)
                  binding.xbaner.setmAdapter { banner, view, position ->

                      Glide.with(banner.context).load("http://117ai.32.hzgqapp.com/"+imageUrls[position]).into(view as ImageView)


                  }
              }else{
                  binding.shiping.visibility=View.VISIBLE

              }
                binding.shipingbiaoti.text=it.data.title
                if(it.data.fine_release_type==1){
                    binding.jingjialeixing.text="已出价：${it.data.pay_price}"
                    binding.xiaotiwo4.visibility=View.GONE
                    binding.jiagemoshi.text="自己定价"

                }else if(it.data.fine_release_type==2){
                    binding.jiagemoshi.text="第二方定价"
                    binding.xiaotiwo4.visibility=View.VISIBLE

                }else{
                    binding.jingjialeixing.text="第二方竞价"
                    binding.jiagemoshi.text="第二方竞价"
                    binding.riqi.visibility=View.VISIBLE
                    binding.xiaotiwo4.visibility=View.VISIBLE
                }

              binding.beizhuneirong.text=it.data.remark
                binding.username.text=it.data.publisher.username

              viewmodel.onwodezhuyewang(UserData.token,mapOf("id" to it.data.publisher.id.toString()))
              if(it.data.endtime==null){
                  binding.riqi.text = ""

              }else{
                  binding.riqi.text = "竞价结束日期："+it.data.endtime

              }

          }else{
              Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

          }

      })



    }
}